SetGfxDriver 1:Graphics 1024,600,8,2 :SetBuffer FrontBuffer() :SeedRnd MilliSecs()

Global st1#,st2#,st3#,st4#,unbit$,fasecnt,maxfase1,minfase2,bit$,ty$,tyold$,tycnt,in$,in1,in2,in3,in4,in31,in41,in5,in6,in51,in61,in7,in8,in71,in81,in9,in10,in91,in101,in11,in12,in111,in121,in13,in14,untest,sort1,sort2,zzz1,hzzz1,jzzz1,yyyyy$,out$
in8 = CreateBank(200):in81 = CreateBank(200):in10 = CreateBank(200):in101 = CreateBank(200):in9 = CreateBank(200):in91 = CreateBank(200):in5 = CreateBank(200):in51 = CreateBank(200):in6 = CreateBank(200):in61 = CreateBank(200):in7 = CreateBank(200):in71 = CreateBank(200)
in1 = CreateBank(5314 ):in2 = CreateBank(531):in3 = CreateBank(200):in4 = CreateBank(200):in31 = CreateBank(200):in41 = CreateBank(200)
Dim decode(4000):Dim encode1(1000000):Dim encode6(3400000)
Dim encode(4000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(80):sort2 = CreateBank(60)
Dim zzz(66666):Dim zzz2(8881,100):Dim hzzz2(8881,100):Dim hzzz(66666):Dim jzzz2(8881,100):Dim jzzz(66666)
decodet():minfase2=800

st1#=1256:st2#=1256:st3#=2:st4#=2
While Not KeyHit(1) Or MouseHit(2)
ClsColor 0,0,0
Cls
randnum(0)
getnums()
;	fact_faza():erase()
;	Color 255,255,210:Text 5,5,bit+"/"+unbit+" "+fasecnt+":  "+ tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", Null cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False
	
;Text 400,10,PeekByte 
a=3
it1=6:uu=7 Xor a Xor 7;Floor((Sin(it1*Pi)*Log(it1*Pi)*163*Cos(it1*14))^it1); Floor((it1^4)*(Sin(it1*14)^0.11)*10)^2+ Floor((it1^3)*(Cos(it1*14)^0.11))^3+ Floor((it1^3)*(Sin(it1*14)^0.11))^2 ;Floor((it1^Pi)*(Sin(it1*6)^0.97)*10)^Pi
Text 510,40,a,False ,False

;checke()
;Color 210,255,210:Text 310,2,in,False ,False
zzz1=zzz1+1:hzzz1=hzzz1+1:jzzz1=jzzz1+1
Flip
If zzz1> 49    Then 
 WaitMouse()
;bit=WaitMouse()
If ((bit<>2) And (tycnt>maxfase1)) Then maxfase1=tycnt
If ((bit=2) And (tycnt<minfase2)) Then minfase2=tycnt	
bit=mod4(bit):
randnum(1):fasecnt=fasecnt+1
EndIf

VWait 
Wend 

Function erase()

		
End Function

Function fact_faza()
;If bit <>2 Then	
t1=Rand(1,4):t2=Rand(1,4):t3=Rand(1,4):t4=Rand(1,4):t5=Rand(1,4):t6=Rand(1,4)
t7=Rand(1,4):t8=Rand(1,4):t9=Rand(1,4):t10=Rand(1,4)

eee1=(t1 Xor t2) Xor t9:eee2=(t2 Xor t1) Xor t10:eee3=(t3 Xor t2) Xor t1
eee4=(t4 Xor t1) Xor t9:eee5=(t5 Xor t4) Xor t1:eee6=(t6 Xor t4) Xor t3
eee7=(t7 Xor t6) Xor t5
eee8=(t8 Xor t5) Xor t2:eee9=(t9 Xor t4) Xor t3:eee10=(t10 Xor t9) Xor t8


;eee1=Rand(1,4):eee2=Rand(1,4):eee3=Rand(1,4):eee4=Rand(1,4):eee5=Rand(1,4):eee6=Rand(1,4):eee7=Rand(1,4):eee8=Rand(1,4):eee9=Rand(1,4):eee10=Rand(1,4):eee11=Rand(1,4):eee12=Rand(1,4)

For it1=1 To 4
For it2=1 To 4
For it3=1 To 4
For it4=1 To 4
For it5=1 To 4
For it6=1 To 4
For it7=1 To 4
For it8=1 To 4
For it9=1 To 4
For it10=1 To 4

ee1= (it1 Xor it2) Xor it9
ee2= (it2 Xor it1) Xor it10
ee3= (it3 Xor it2) Xor it1
ee4= (it4 Xor it1) Xor it9
ee5= (it5 Xor it4) Xor it1
ee6= (it6 Xor it4) Xor it3
ee7= (it7 Xor it6) Xor it5
ee8= (it8 Xor it5) Xor it2
ee9= (it9 Xor it4) Xor it3
ee10= (it10 Xor it9) Xor it8


If  (ee1=eee1) And (ee2=eee2)And (ee3=eee3)And(ee4=eee4) And (ee5=eee5)And (ee6=eee6)And (ee7=eee7)And (ee8=eee8)And (ee9=eee9)And (ee10=eee10)Then zzz(zzz1)=zzz(zzz1)+1:zzz2(zzz1,zzz2(zzz1,100))=it1:its=it1:zzz2(zzz1,100)=zzz2(zzz1,100)+1
		;If zzz2(zzz1,100)=0 Then 
		zzz2(zzz1,99)=eee:its=eee
	Next
Next
Next
	
	Next	
	Next
	Next
	Next
	Next
	Next
	Next
	For t=0 To 728*4 Step 72:PokeInt in2,t,mod1(mod1(PeekInt(in2,t)-it1^11)):Next		
	For t=0 To 728*4 Step 72:PokeInt in2,t,mod1(mod1(PeekInt(in2,t)-it1^11)):Next	
	
	For it1=1 To 6
	For t=0 To 728*4 Step 4:PokeInt in1,t,PeekInt(in2,t):Next		
	For t=0 To 728*4 Step 72:PokeInt in1,t,mod1(mod1(PeekInt(in2,t)+it1^11)):Next		
	
;	t1=0:For t=0 To 6560*4 Step 36
;eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
;	PokeInt in1,t1,eee:t1=t1+4
;	Next		

	t1=0:For t=0 To 728*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		

	t1=0:For t=0 To 80*4 Step 36
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))
	PokeInt in1,t1,eee:t1=t1+4
	Next		
t=0
eee=1+fact(fact(PeekInt(in1,t+4*0),PeekInt(in1,t+4*4),PeekInt(in1,t+4*9)),fact(PeekInt(in1,t+4*1),PeekInt(in1,t+4*5),PeekInt(in1,t+4*10)),fact(PeekInt(in1,t+4*2),PeekInt(in1,t+4*6),PeekInt(in1,t+4*12)))


		If it1= eee Then jzzz(jzzz1)=jzzz(jzzz1)+1:jzzz2(jzzz1,jzzz2(jzzz1,100))=it1:jzzz2(jzzz1,100)=jzzz2(jzzz1,100)+1
		If jzzz2(jzzz1,100)=0 Then jzzz2(jzzz1,99)=eee

	Next			
;Next					
				
;Else


	
;EndIf 	



End Function


























Function mod2(x)
While x>6
 x = x -6
Wend 
While  x<1 
 x = x +6
Wend 
Return x
End Function

Function mod1(x)
While x>1073741823
 x = x -1073741824
Wend 
While  x<0 
 x = x +1073741824
Wend 
Return x
End Function

Function randnum(bt)

For i = 0 To 72 Step 4
t = Rand(0,7):PokeInt in1,i, t:PokeInt in2,i, t;:PokeInt in3,i*4, t:PokeInt in4,i*4, t:PokeInt in5,i*4, t
Next

If bt=1 Then
For i=0 To 66666
zzz1=0:jzzz1=0:hzzz1=0:tycnt=0
hzzz(i)=0:zzz(i)=0:jzzz(i)=0
Next
For i1=0 To 8881
For i2=0 To 100
jzzz2(i1,i2)=0
zzz2(i1,i2)=0
hzzz2(i1,i2)=0
Next
Next
EndIf

End Function


Function getnums()
	For y=0 To 2
	For x=0 To 2
		Color 0,255,0:Text 30+x*47,y*9+10,PeekInt(in1,(y*7+x)*4),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10)):Text 150,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,0+15),PeekInt(in1,5+15),PeekInt(in1,10+15)):Text 150,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,0+2*15),PeekInt(in1,5+2*15),PeekInt(in1,10+2*15)):Text 150,28,xx3,False ,False 
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,22,lxx1,False ,False 
	
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,0+15),PeekInt(in1,0+2*15)):Text 180,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,5),PeekInt(in1,5+15),PeekInt(in1,5+2*15)):Text 180,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,10),PeekInt(in1,10+15),PeekInt(in1,10+2*15)):Text 180,28,xx3,False ,False 
;	lxx2=fact(xx1,xx2,xx3)
	Text 190,22,lxx2,False ,False:Text 210,22,lxx1*6+lxx2,False ,False 

	fact_faza()
	erase()
	
	For y=0 To 2
	For x=0 To 2
	Color 255,255,0:Text 230+x*16,y*9+10,PeekByte(in2,(y*7+x)),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
;	xx2=fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
;	xx3=fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,62,lxx1,False ,False 

;	xx1=fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15))
;	xx2=fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15))
;	xx3=fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15))
;	lxx2 = fact(xx1,xx2,xx3)
	Text 190,62,lxx2,False ,False :Text 210,62,lxx1*6+lxx2,False ,False 	
	
;         STAT
;	For i=0 To hzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+hzzz(i)
;		Color 155,255,0:Text 15+(i*19) Mod 960,180+68*Int(i/80),Str(hzzz(i))+",",False ,False 
;			For i9=0 To hzzz2(i,100)-1
;			Color hzzz2(i,i9)*21,hzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,180+68*Int(i/80)+i9*10+10,Str(hzzz2(i,i9))+"",False ,False 	
;			Next	
;		If hzzz2(i,100) = 0 Then 
;		Color 155,hzzz2(i,99)*51,hzzz2(i,99)*51:Text 15+(i*19) Mod 960,180+68*Int(i/80)-7,Str(hzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
;
	For i=0 To jzzz1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+jzzz(i)
		Color 105,255,0:Text 15+(i*9) Mod 960,155+68*Int(i/100),Str(jzzz(i))+",",False ,False 
			For i9=0 To jzzz2(i,100)-1
			Color jzzz2(i,i9)*21,jzzz2(i,i9)*51,155:Text 15+(i*9) Mod 960,155+68*Int(i/100)+i9*10+10,Str(jzzz2(i,i9))+"",False ,False 	
			Next	
		If jzzz2(i,100) = 0 Then 
		Color 105,jzzz2(i,99)*51,jzzz2(i,99)*51:Text 15+(i*9) Mod 960,155+68*Int(i/100)-7,Str(jzzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	
	ttt0=0:wnullmax=0
	For i=0 To zzz1
		If zzz(i) = 0 Then 
			cnull = cnull +1
			If wnullmax < wnullmax0 Then wnullmax = wnullmax0
			wnullmax0=0
		EndIf
		If zzz(i) <> 0 Then wnullmax0=wnullmax0+1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+zzz(i)
		Color 255,255,0:Text 15+(i*19) Mod 950,110+68*Int(i/100),Str(zzz(i))+",",False ,False 
			For i9=0 To zzz2(i,100)-1
			Color zzz2(i,i9)*51,zzz2(i,i9)*51,255:Text 15+(i*19) Mod 950,110+68*Int(i/100)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
			Next	
		If zzz2(i,100) = 0 Then 
		Color 255,zzz2(i,99)*51,zzz2(i,99)*51:Text 15+(i*19) Mod 950,110+68*Int(i/100)-7,Str(zzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	cnull = Ceil(cnull * 100 /  (zzz1+1))
	tnull = Ceil(tycnt* 100 /  (zzz1+1))

	Color 255,0,0:
	If bit=2 Text 140,89,"-",False ,False Else Color 0,255,0:Text 70,89,"+"

	Color 255,255,210:Text 10,95,tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", One cnt=" + cnull+"%, max without Null="+wnullmax+", phase=4",False ,False

End Function


Function fact(x1,x2,x3)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1
	For x=3 To 1 Step -1
	maxx =0
	For xt=0 To 2
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5))
	ttt=ttt+1
	xx = encode((ttt-1)*5)
	Return xx
End Function

Function fact6(x1,x2,x3,x4,x5,x6)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1:aaa(15)=x4+1:aaa(20)=x5+1:aaa(25)=x6+1
	For x=6 To 1 Step -1
	maxx =0
	For xt=0 To 5
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5) +""+PeekByte (sort1,3*5) +""+PeekByte (sort1,4*5) +""+PeekByte (sort1,5*5) )
	ttt=ttt+1
	xx = encode6((ttt-1)*5)
	Return xx
End Function

Function mod4(x)
If x =2 Then x =1: Else x=2
Return x
End Function

Function decodet()
y=0
For x1=1 To 3
For x2=1 To 3
For x3=1 To 3
If((x1 <> x2) And (x1<>x3) And (x2<>x3)) decode(y) =x1+""+x2+""+x3:encode(Int(x1+""+x2+""+x3)*5)=y:y=y+1
Next
Next
Next


End Function